<?php

/**
 * @filename hsw.class 
 * @encoding UTF-8 
 * @author  fwz 
 * @datetime 2018-7-5  10:56:11
*/


class hsw{
    
    private  $serv = null;
    public function __construct() {
        
        File::init();
        $this->serv = new swoole_websocket_server("0.0.0.0",9501);
        $this->serv->set(array('task_worker_num'=>8));
        
        $this->serv->on("open",array($this,'onOpen'));  //open 建立链接
        $this->serv->on("message",array($this,'onMessage')); //message 接受数据
        $this->serv->on("Task",array($this,'onTask'));       ////处理异步任务
        $this->serv->on("Finish",array($this,'onFinish'));   //异步处理结果
        $this->serv->on("close",array($this,'onClose'));     //关闭
        
        $this->serv->start();
        
    }
    
    public function onOpen($serv,$request){
        $data = array('task'=>"open",'fd'=>$request->fd);
        $this->serv->task(json_encode($data));
        echo "open \n";
    }
    
    
    public function onMessage($serv,$frame){
        $data = json_decode($frame->data,TRUE);
        
        switch ($data['type']) {
            case 1: //登陆
                $redata = array(
                    'task'=>'login',
                    'params'=>array(
                        'email'=>$data['email'],
                        'name'=>$data['name']
                    ),
                    'fd'=>$frame->fd,
                    'roomid'=>$data['roomid']
                );
                if(!$redata['params']['email'] || !$redata['params']['name'] ){
                    $redata['task'] = 'nologin';
                    $this->serv->task(json_encode($redata));
                }
                $this->serv->task(json_encode($redata));
                break;
            case 2: //新消息
                $redata = array(
                    'task'=>'new',
                    'params'=>array(
                        'name'=>$data['name'],
                        'email'=>$data['email']
                    ),
                    'c'=>$data['c'],
                    'message'=>$data['message'],
                    'fd'=>$frame->fd,
                    'roomid'=>$data['roomid']
                );
                
                $this->serv->task(json_encode($redata));
                break;
            case 3: //改变房间
                $redata = array(
                    'task'=>'change',
                    'params'=>array(
                        'name'=>$data['name'],
                        'email'=>$data['email']
                    ),
                    'fd'=>$frame->fd,
                    'oldroomid'=>$data['oldroomid'],
                    'roomid'=>$data['roomid']
                );
                
                $this->serv->task(json_encode($redata));
                break;
            default:
                $this->serv->push($frame->fd, json_encode(array('code'=>0,'msg'=>'type error')));
                break;
        }
    }
    
    // 处理异步任务
    public function onTask($serv,$task_id,$from_id,$data){
        $pushmsg = array('code'=>0,'msg'=>"",'data'=>array());
        $data = json_decode($data,true);
        
        switch ($data['task']) {
            case 'open':
                $pushmsg = Chat::open($data);
                $this->serv->push($data['fd'],  json_encode($pushmsg));
                
                break;
            case 'login':
                $pushmsg = Chat::dologin($data);
                
                break;
            case 'new': 
                $pushmsg = Chat::sendNewMsg($data);
                
                break;
            case 'logout':
                $pushmsg = Chat::doLogout($data);
                break;
            case 'nologin':
                $pushmsg = Chat::noLogin($data);
                break;
            case 'change':
                $pushmsg = Chat::change($data);
                break;            
        }
        $this->sendMsg($pushmsg, $data['fd']);
        return "finish";
    }
    // 处理异步任务
    public function onFinish($serv,$task_id,$data){
       echo "task $task_id finish \n";
       echo "result $data \n ";
    }
    
    
    public function onClose($serv,$fd){
        $pushmsg = array();
        
        $user = Chat::logout("",$fd);
        if($user){
            $data = array(
                'task'=>'logout',
                'params'=>array(
                    'name'=>$user['name']
                ),
                'fd'=>$fd
            );
            $this->serv->task( json_encode($data) );
        }
        echo "client $fd closed \n";
    }
    
    
    
    
    public  function sendMsg($pushmsg,$myfd){
        foreach ($this->serv->connections as $fd) {
            
            if($myfd == $fd){
                $pushmsg['data']['mine'] = 1;
            }else{
                $pushmsg['data']['mine'] = 0;
            }
            $this->serv->push($fd,  json_encode($pushmsg));
        }
    }
    
}